//
//  BSKBookModeViewController.m
//  MicroMemory
//
//  Created by rimi on 16/7/22.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "BSKBookModeViewController.h"
#import "BSKNote.h"
#import "BSKBookContentViewController.h"
#import "BSKconsts.h"
#import "BSKTheme.h"
#import "BSKDerectorySelecterView.h"
#import "UIViewController+BSKToast.h"
@interface BSKBookModeViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
@property(nonatomic,strong) UIPageViewController * pageVC;
@property(nonatomic,assign) NSInteger curentIndex;
@property(nonatomic,strong) UIImageView * noteBGimageView;
@property(nonatomic,strong) UIButton * backButton ;
@property(nonatomic,strong) UIButton * derectoryButton ;
@property(nonatomic,assign) BOOL pageWillAdd;
@end

@implementation BSKBookModeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	_curentIndex = 0;
	self.view.backgroundColor = [BSKTheme curentTheme].themeColor;
	_noteBGimageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"noteBookBG"]];
	_noteBGimageView.frame = CGRectMake(0, StatusBarHeight, ScreenWidth, ScreenHeight - StatusBarHeight*2+5);
	[self.view addSubview:_noteBGimageView];
	_noteBGimageView.userInteractionEnabled =YES;
	_pageVC = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
	_pageVC.dataSource = self;
	_pageVC.delegate = self;
    // Do any additional setup after loading the view.
	BSKBookContentViewController * firstCOntentVC = [self controllerAtIndex:0];
	NSArray * controllers = @[firstCOntentVC];
	[_pageVC setViewControllers:controllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
	_pageVC.view.frame = CGRectMake(0, StatusBarHeight, ScreenWidth - 10, ScreenHeight -StatusBarHeight*2);
	[self addChildViewController:_pageVC];
	[self.view addSubview:_pageVC.view];
	[_pageVC didMoveToParentViewController:self];
	
	_backButton  = [UIButton buttonWithType:UIButtonTypeCustom];
	[_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
	_backButton.frame = CGRectMake(5, StatusBarHeight+5, NavigationBarHeight -10, NavigationBarHeight -10);
	_backButton.imageEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
	[_backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
	_backButton.backgroundColor = [BSKTheme curentTheme].themeColor;
	_backButton.layer.cornerRadius = (NavigationBarHeight -10)/2;
	[self.view addSubview:_backButton];
	
	_derectoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[_derectoryButton setImage:[UIImage imageNamed:@"directory"] forState:UIControlStateNormal];
	_derectoryButton.frame = CGRectMake(NavigationBarHeight, StatusBarHeight+5, NavigationBarHeight -10, NavigationBarHeight -10);
	_derectoryButton.imageEdgeInsets = UIEdgeInsetsMake(6, 6, 6, 6);
	[_derectoryButton addTarget:self action:@selector(derectoryButtonAction:) forControlEvents:UIControlEventTouchUpInside];
	_derectoryButton.backgroundColor = [BSKTheme curentTheme].themeColor;
	_derectoryButton.layer.cornerRadius = (NavigationBarHeight -10)/2;
	[self.view addSubview:_derectoryButton];
}

-(void)derectoryButtonAction:(UIButton*)sender{
	BSKDerectorySelecterView * selecterView = [[BSKDerectorySelecterView alloc]initWithNoteList:_noteList selectedCallBack:^(NSInteger index) {
		[_pageVC setViewControllers:@[[self controllerAtIndex:index]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
			_curentIndex = index;
		}];
	}];
	[self.view addSubview:selecterView];
	selecterView.titleLabel.text = @"目录";
	[selecterView show];
	NSLog(@"%@",_pageVC.viewControllers);
}
-(void)backAction:(UIButton*)sender{
	[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark UIPageViewControllerDataSource
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
	if (_curentIndex -1 < 0 || _curentIndex == NSNotFound || _noteList.count <= 0) {
		[self MakeToast:@"已经是第一页了！" WithTime:0.5];
		return nil;
	}
	_pageWillAdd=NO;
	NSLog(@"-%ld",(long)_curentIndex);
	return [self controllerAtIndex:_curentIndex-1];}
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
	if ( _curentIndex +1 >= _noteList.count || _curentIndex == NSNotFound || _noteList.count <= 0) {
		[self MakeToast:@"已经是最后一页了！" WithTime:0.5];
		return nil;
	}
	_pageWillAdd = YES;
	NSLog(@"+%ld",(long)_curentIndex);
	return [self controllerAtIndex:_curentIndex+1];
}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
	
	if (completed) {
		if (_pageWillAdd) {
			_curentIndex++;
			if (_curentIndex>=_noteList.count) {
				_curentIndex = _noteList.count -1;
			}
		}
		else{
			_curentIndex--;
			if (_curentIndex < 0) {
				_curentIndex = 0;
			}
		}
	}
}

-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{
	return _noteList.count;
}

-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
	return 0;
}

-(BSKBookContentViewController *)controllerAtIndex:(NSInteger)index{
	BSKBookContentViewController * contentVC = [[BSKBookContentViewController alloc]initWithBounds: CGRectMake(0, 0, ScreenWidth - 10, ScreenHeight - StatusBarHeight*2) ];
	
	NSInteger curentPage = index+1;
	
	if (_noteList.count > 0 ) {
		BSKNote* note = _noteList[_noteList.count - 1-index];
		[contentVC setVCWithNote:note index:curentPage indexCount:_noteList.count];
	}

	return contentVC;
}

@end
